//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package service.impl;

import dao.UserDao;
import dao.impl.UserDaoImpl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import model.User;
import service.UserService;
import utils.JDBCUtils;

public class UserServicelmpl implements UserService {
    UserDao userDao = new UserDaoImpl();

    public UserServicelmpl() {
    }

    public User getUserByName(String username) throws SQLException {
        String sql = "select *from t_user where username = ?";
        ResultSet resultSet = JDBCUtils.executeQuery(sql,new Object[]{username});
        User user = null;
        while(resultSet.next()){
            user = new User();
            String currentuser = resultSet.getString("username");
            String pass = resultSet.getString("password");
            System.out.println(currentuser + " " + pass);
            user.setUsername(currentuser);
            user.setPassword(pass);
        }
        return user;
    }

    public ArrayList<User> getAllUser() {
        return this.userDao.getAllUser();
    }

    public int addUser(User user) {
//        return this.userDao.addUser(user);
        String username = user.getUsername();
        String pass = user.getPassword();
        String sql ="insert into t_user (username,password) values(?,?)";
        return JDBCUtils.executeUpdate(sql,new Object[]{username,pass});
    }
}
